package com.brave.user.controller;

import com.brave.base.BaseController;
import com.brave.base.CheckNullParamKit;
import com.brave.base.MobileKit;
import com.brave.base.RedisKit;
import com.brave.user.dto.DoLoginDTO;
import com.brave.user.dto.RedisUserInfo;
import com.brave.user.entity.MCUserSignInfoEntity;
import com.brave.user.repository.MCUserSignInfoRepository;
import com.brave.user.service.UserQueryService;
import com.brave.user.service.UserUpdateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Api(value = "用户部分接口", tags = "用户部分接口", description = "用户接口")
@RestController
@RequestMapping("/v1/user")
public class UserQueryController extends BaseController
{
    @Autowired
    private RedisKit redisKit;
    @Autowired
    UserQueryService userQueryService;
    @Autowired
    UserUpdateService userUpdateService;
    
    
    @ApiOperation(value = "用户-登录")
    @ApiImplicitParam(name = "dto", value = "登录实体", required = true, dataType = "DoLoginDTO")
    @PostMapping(value = "/doLogin", produces = "application/json")
    public String doLogin(@RequestBody DoLoginDTO dto)
    {
        //执行登录操作
        Map<String, String> checkMap = CheckNullParamKit.checkParam(dto);
        if(0 != checkMap.size())
        {
            return badParameter(checkMap);
        }
        //调用业务逻辑
        return userUpdateService.doLogin(dto);
    }
    
    
    
    @ApiOperation(value = "用户-查询手机号是否被占用")
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "string", paramType = "query")
    @GetMapping(value = "/findPhoneExist", produces = "application/json")
    public String findPhoneExist(@RequestParam("phone") String phone)
    {
        //验证手机号码格式是否正确
        boolean checkResult = MobileKit.checkMobile(phone);
        if(!checkResult)
        {
            Map<String, String> checkMap = new HashMap<>(2);
            checkMap.put("phone", "手机号码格式不正确.");
            return badParameter(checkMap);
        }
        //调用业务逻辑
        return userQueryService.findPhoneExist(phone);
    }



    @ApiOperation(value = "用户-我的邀请二维码")
    @GetMapping(value = "/findInviteQRCode", produces = "application/json")
    public String findInviteQRCode(@RequestHeader String token)
    {
        //验证手机号码格式是否正确
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //调用业务逻辑
        return userQueryService.findInviteQRCode(redisUserInfo);
    }


    
    @ApiOperation(value = "查询用户基础信息")
    @GetMapping(value = "/findUserInfo", produces = "application/json")
    public String findUserInfo(@RequestHeader String token)
    {
        //校验身份
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //调用业务逻辑
        return userQueryService.findUserInfo(redisUserInfo);
    }
    
    @Autowired
    private MCUserSignInfoRepository signInfoRepository;
    
    
    @ApiOperation(value = "生成签到明细表数据")
    @GetMapping(value = "/gen", produces = "applciation/json")
    public String gen()
    {
        Date now = new Date();
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday;

        for(int j = 0; j < 39; j ++)
        {
            for(int i = 1; i <= cale.getMaximum(Calendar.DAY_OF_MONTH); i ++)
            {
                cale = Calendar.getInstance();
                cale.add(Calendar.MONTH, j);
                cale.set(Calendar.DAY_OF_MONTH, i);
                firstday = format.format(cale.getTime());

                MCUserSignInfoEntity info = new MCUserSignInfoEntity();
                info.setSignDate(format.format(cale.getTime()));
                info.setSort(i);
                info.setYear(cale.get(Calendar.YEAR));
                info.setMonth(cale.get(Calendar.MONTH) + 1);
                info.setCreateDate(now);
                signInfoRepository.save(info);
            }
        };
        return success();
    }


    @ApiOperation(value = "查询用户签到明细")
    @GetMapping(value = "/findUserSignDetail", produces = "application/json")
    public String findUserSignDetail(@RequestHeader String token)
    {
        //用户信息获取
        RedisUserInfo redisUserInfo = redisKit.checkUserToken(token);
        if(null == redisUserInfo)
        {
            return tokenBadRequest();
        }
        //调用逻辑
        return userQueryService.findUserSignDetail(redisUserInfo);
    }
    
    
    
    
    
    
}